<template>
  <el-card>
    <template #header>店铺管理</template>

    <!-- 1.0 店铺的表单布局 -->
    <el-form
      :model="shopForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
      size="mini"
    >
      <!-- 输入框 -->
      <el-form-item label="店铺名称" prop="name">
        <el-input v-model="shopForm.name"></el-input>
      </el-form-item>
      <!-- 文本域 -->
      <el-form-item label="公告" prop="bulletin">
        <el-input type="textarea" v-model="shopForm.bulletin"></el-input>
      </el-form-item>
      <!-- 店铺头像 -->
      <el-form-item label="店铺头像">
        <el-upload
          class="avatar-uploader"
          action="http://127.0.0.1:5000/shop/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <!-- 店铺图片 -->
      <el-form-item label="商品图片">
        <el-upload
          action="http://127.0.0.1:5000/shop/upload"
          list-type="picture-card"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
          :on-success="handleShoparrSuccess"
          :file-list="picArr"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="" />
        </el-dialog>
      </el-form-item>

      <!-- 输入框 -->
      <el-form-item label="配送费" prop="deliveryPrice">
        <el-input v-model="shopForm.deliveryPrice"></el-input>
      </el-form-item>
      <!-- 输入框 -->
      <el-form-item label="配送时间" prop="deliveryTime">
        <el-input v-model="shopForm.deliveryTime"></el-input>
      </el-form-item>
      <!-- 输入框 -->
      <el-form-item label="店铺描述" prop="description">
        <el-input v-model="shopForm.description"></el-input>
      </el-form-item>
      <!-- 输入框 -->
      <el-form-item label="店铺评分" prop="score">
        <el-input v-model="shopForm.score"></el-input>
      </el-form-item>
      <!-- 输入框 -->
      <el-form-item label="销量" prop="sellCount">
        <el-input v-model="shopForm.sellCount"></el-input>
      </el-form-item>

      <!-- 活动 -->
      <el-form-item label="活动">
        <el-checkbox-group v-model="shopForm.supports">
          <el-checkbox label="在线支付满28减5"></el-checkbox>
          <el-checkbox label="VC无限橙果汁全场8折"></el-checkbox>
          <el-checkbox label="单人精彩套餐"></el-checkbox>
          <el-checkbox label="特价饮品8折抢购"></el-checkbox>
          <el-checkbox label="单人特色套餐"></el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <!-- 时间选择 -->
      <el-form-item label="营业时间">
        <el-time-picker
          is-range
          v-model="shopForm.date"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          placeholder="选择时间范围"
        >
        </el-time-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确认修改</el-button
        >
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
//4.1 获取店铺详情的方法
//4.2 店铺内容修改的方法
import { getInfo, getEdit } from '../../api/shop'
export default {
  data() {
    return {
      imageUrl: '',
      dialogImageUrl: '',
      dialogVisible: false,
      picArr: [],
      // 数据对象
      shopForm: {
        name: '',
        bulletin: '',
        avatar: '',
        deliveryPrice: '',
        deliveryTime: '',
        description: '',
        score: '',
        sellCount: '',
        supports: [],
        date: []
      },
      // 规则对象
      rules: {
        name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        bulletin: [
          { required: true, message: '请输入公告', trigger: 'blur' },
          {
            min: 5,
            max: 120,
            message: '长度在 5 到 120 个字符',
            trigger: 'blur'
          }
        ],
        deliveryPrice: [
          { required: true, message: '请输入公告', trigger: 'blur' }
        ],
        deliveryTime: [
          { required: true, message: '请输入公告', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入公告', trigger: 'blur' }
        ],
        score: [{ required: true, message: '请输入公告', trigger: 'blur' }],
        sellCount: [{ required: true, message: '请输入公告', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getDate()
  },
  methods: {
    async getDate() {
      const { data } = await getInfo()
      // console.log(data)
      this.shopForm = data
      this.imageUrl = 'http://127.0.0.1:5000/upload/shop/' + data.avatar

      data.pics.forEach(item => {
        this.picArr.push({
          name: item,
          url: 'http://127.0.0.1:5000/upload/shop/' + item
        })
      })
    },

    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          // 验证成功
          const obj = JSON.parse(JSON.stringify(this.shopForm))
          obj.date = JSON.stringify(obj.date)
          obj.supports = JSON.stringify(obj.supports)
          obj.pics = JSON.stringify(obj.pics)
          console.log(obj, 'obj')

          const { code, msg } = await getEdit(obj)
          if (code === 0) {
            this.$message.success(msg)
            setTimeout(() => {
              location.reload()
            }, 1500)
          } else {
            this.$message.error(msg)
          }
        } else {
          // console.log('error submit!!')
          return false
        }
      })
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      console.log(res, '图片')
      this.shopForm.avatar = res.imgUrl
    },
    handleShoparrSuccess(res) {
      this.shopForm.pics.push(res.imgUrl)
    },
    beforeAvatarUpload(file) {
      // const isJPG = file.type === 'image/jpeg'
      const arr = ['image/jpeg', 'image/jpg', 'image/png', 'image/gif']
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!arr.includes(file.type)) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return arr.includes(file.type) && isLt2M
    },
    handleRemove(file, fileList) {
      // console.log(this.picArr, '显示')
      const index01 = this.shopForm.pics.findIndex(item => item === file.name)
      this.shopForm.pics.splice(index01, 1)
      const index02 = this.picArr.findIndex(item => item.name === file.name)
      this.picArr.splice(index02, 1)
      // console.log(file)
      // console.log(this.shopForm.pics, '数组')
      // console.log(this.picArr, '显示')
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    }
  }
}
</script>

<style lang="less" scoped>
.el-input {
  width: 178px;
}

/deep/textarea {
  width: 400px;
  height: 80px;
  resize: none;
}

/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
/deep/.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 128px;
  height: 128px;
  line-height: 128px;
  text-align: center;
}
.avatar {
  width: 128px;
  height: 128px;
  display: block;
}
</style>
